package com.example.downloader.utils;

/**
 * Created by Administrator on 2017/9/21 0021.
 */

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.content.ContextCompat;

/**
 * Created by Administrator on 2017/8/4 0004.
 */

public class PermissionsCheckUtils {
    public static final int REQUEST_DODNLOAD = 5;

    public static final String[] STORAGE_PERMISSIONS = {Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE};



    static boolean lacksPermissions(Context context, String... permissions) {
        for (String permission : permissions) {
            if (lacksPermission(context, permission)) {
                return true;
            }
        }
        return false;
    }

    public static boolean lackAndRequestPermissions(ActivityContext activity, int requestCode, String... permissions) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return false;
        }
        if (lacksPermissions(activity.getActivity(), permissions)) {
            requestPermissions(activity, requestCode, permissions);
            return true;
        }
        return false;
    }


    private static void requestPermissions(ActivityContext context, int requestCode, String... permissions) {
        context.requestPermissions(permissions, requestCode);
    }

    static private boolean lacksPermission(Context context, String permission) {
        return ContextCompat.checkSelfPermission(context, permission) ==
                PackageManager.PERMISSION_DENIED;
    }

    public static boolean hasAllPermissionsGranted(int[] grantResults) {
        if (grantResults.length > 0) {
            for (int grantResult : grantResults) {
                if (grantResult == PackageManager.PERMISSION_DENIED) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }
}